project(h2curl)
cmake_minimum_required(VERSION 3.0)
include(ExternalProject)

option(CURL_STATICLIB "CURL use static library" OFF)
option(BUILD_CURL_ONLY "Build CURL only" ON)

set(NGHTTP_INSTALL_ARG
  INSTALL_COMMAND make install
)
set(NGHTTP_LIB_ONLY)
set(NGHTTP_PREFIX ${CMAKE_INSTALL_PREFIX})
if(BUILD_CURL_ONLY)
  set(NGHTTP_INSTALL_ARG)
  set(NGHTTP_LIB_ONLY
    -DENABLE_LIB_ONLY:BOOL=ON
  )
  set(NGHTTP_PREFIX "${CMAKE_BINARY_DIR}/nghttp2_install")
endif()

ExternalProject_Add(nghttp2
  SOURCE_DIR ${CMAKE_SOURCE_DIR}/nghttp2
  PREFIX ${CMAKE_BINARY_DIR}/nghttp2
  CMAKE_ARGS
    -DCMAKE_PROJECT_nghttp2_INCLUDE=${CMAKE_SOURCE_DIR}/cmake/nghttp2.cmake
    -DCMAKE_INSTALL_PREFIX:PATH=${NGHTTP_PREFIX}
    -DCMAKE_BUILD_TYPE:STRING=${CMAKE_BUILD_TYPE}
    ${NGHTTP_LIB_ONLY}
    BUILD_COMMAND make
    ${NGHTTP_INSTALL_ARG}
)
set(NGHTTP2INCLUDE "${CMAKE_SOURCE_DIR}/nghttp2/lib/includes")

set(NGHTTP2LIB "${CMAKE_BINARY_DIR}/nghttp2/src/nghttp2-build/lib/libnghttp2.a")

ExternalProject_Add(CURL
  DEPENDS nghttp2
  SOURCE_DIR ${CMAKE_SOURCE_DIR}/curl
  PREFIX ${CMAKE_BINARY_DIR}/curl
  CMAKE_ARGS
    -DCMAKE_PROJECT_CURL_INCLUDE=${CMAKE_SOURCE_DIR}/cmake/curl.cmake
    -DCMAKE_INSTALL_PREFIX:PATH=${CMAKE_INSTALL_PREFIX}
    -DCMAKE_BUILD_TYPE:STRING=${CMAKE_BUILD_TYPE}
    -DNGHTTP2_INCLUDE_DIR:STRING=${NGHTTP2INCLUDE}
    -DNGHTTP2_LIBRARY:STRING=${NGHTTP2LIB}
    -DUSE_NGHTTP2:BOOL=ON
    -DCURL_STATICLIB:BOOL=${CURL_STATICLIB}
  BUILD_COMMAND make
  INSTALL_COMMAND make install
)
